<?php
//用户


namespace app\adminbackstage\controller;
use app\BaseController;
use app\Response;
use think\facade\Session;
use think\facade\Request;
use think\facade\Cache;
use app\adminbackstage\model\User;
/**
 * Description of UserController
 *
 * @author asus80
 */
class UserController extends BaseController{
    //put your code here

    public function head(){
        $username = session('username');

        $user_type = session('type');
        
        $data['u_name'] = $username;
        $data['u_type'] = $user_type;
        echo  json_encode($data);
        
        // 模板变量赋值
//        View::assign('name','ThinkPHP');
//        View::assign('email','thinkphp@qq.com');
//        return View::fetch('index');
    }
    
    
    public function login(){

        if(Request::method() == 'POST'){
            $username = Request::param('user');
            $password = Request::param('pw');
            $db = new User();
            $where[] = ["username|phone","=",$username];
            $find = $db->info($where);
            if(!empty($find)){
                $find = $find->toArray();
                $encryption = $find['encryption'];
            }
            else{
                return Response::wrapError('用户名密码错误');
            }
            if($find['type'] != 1){
                return Response::wrapError('该用户不为管理员');
            }
            $password = $this->encPassword($password,$encryption);//密码
            $find_info = $db->find_info($username,$password);
            if(!$find_info){
                return Response::wrapError('用户名密码错误');
            }
            if(!empty($find_info)){
                $find_info = $find_info->toArray();
            }
            if($find_info['enable'] == 0){
                return Response::wrapError('账号被锁定');
            }
            $db->lasttime($find_info['uid']);
            session('uid', $find_info['uid']);
            session('username', $username);
            session('type', $find_info['type']);
            $info = [];
            $info['uid'] = $find_info['uid'];
            $info['username'] =  $username;
            $info['type'] =  $find_info['type'];
            $newStr = self::str_random(8);
            if (Cache::has("admin-token".$info["uid"])){
                Cache::set("admin-token".$info["uid"],"");
                Cache::set("admin-token".$info["uid"],$newStr,43200);
            }else{
                Cache::set("admin-token".$info["uid"],$newStr,43200);
            }
            $info['auth_token'] = $newStr;
            return Response::wrapData($info);
        }
    }
    
    
    //获取session
    public function user_session($name='uid'){

        $session = session($name);
        if($session){
            echo $session;
        }
        else{
            echo 0;
        }
    }
    
    //退出
    public function user_exit(){
        Session::clear();
    }

    /**
     * 生成随机字符串
     * @param int $user_id
     * @param array $data
     * @return string
     */
    public static function str_random($length = 16)
    {
        $string = '';
        while (($len = strlen($string)) < $length) {
            $size = $length - $len;
            $bytes = random_bytes($size);
            $string .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $size);
        }
        return $string;
    }
    
}
